package com.baizhi.controller;

import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import com.baizhi.annotation.LogAnnotation;
import com.baizhi.entity.Guru;
import com.baizhi.service.GuruService;
import org.apache.commons.lang3.builder.ReflectionToStringBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Controller
@RestController("/guru")
public class GuruController {

    @Autowired
    private GuruService guruService;



    @RequestMapping("/selectAllGuru")
    @ResponseBody
    @LogAnnotation(value = "查询所有上师信息",type = "select")
    public Map selectAllGuru(int page ,int limit,String guruName){
        return guruService.selectAllGuru(page,limit,guruName);
    }

    @RequestMapping("/updateGuruStatus")
    @ResponseBody
    @LogAnnotation(value = "上师的隐藏or展示",type = "update")
    public Map updateGuruStatus(Guru guru){
        return guruService.updateGuruStatus(guru);
    }

    @RequestMapping("/selectByGuruId")
    @ResponseBody
    @LogAnnotation(value = "查询一个上师信息",type = "select")
    public Guru selectByGuruId(int guruId){
        return guruService.selectByGuruId(guruId);
    }


    @RequestMapping("/updateGuru")
    @ResponseBody
    @LogAnnotation(value = "修改上师",type = "update")
    public Map updateGuru(Guru guru){
        return guruService.updateGuru(guru);
    }


    @RequestMapping("/updateGuru1")
    @ResponseBody
    @LogAnnotation(value = "修改上师",type = "update")
    public Map updateGuru1(Guru guru){
        return guruService.updateGuru1(guru);
    }

    @RequestMapping("/insertGuru")
    @ResponseBody
    @LogAnnotation(value = "添加上师信息",type = "insert")
    public Map insertGuru(Guru guru, MultipartFile file, HttpServletRequest request) throws IOException {
        System.out.println(guru);
        System.out.println(file);
        String realPath=request.getServletContext().getRealPath("/img");//绝对路径
        System.out.println(realPath);
        //做重命名处理，防止文件同名被覆盖

        String newFileName=  new Date().getTime()+"_"+file.getOriginalFilename();
        guru.setGuruImage("/img/"+newFileName);
        file.transferTo(new File(realPath+"/"+newFileName));
        return guruService.insertGuru(guru);
    }

    @RequestMapping("/importExcel")
    public String importExcel(MultipartFile file) throws Exception {
        System.out.println(file);
        ImportParams importParams = new ImportParams();
        importParams.setHeadRows(1);
        importParams.setTitleRows(1);
        //是否需要保存上传的Excel
        importParams.setNeedSave(true);
        List<Guru> list = ExcelImportUtil.importExcel(file.getInputStream(), Guru.class, importParams);
        for(Guru guru:list){
            System.out.println(ReflectionToStringBuilder.toString(guru));
            System.out.println(guru);
        }
        return "index";

    }

    @RequestMapping("/deleteGuru")
    @ResponseBody
    @LogAnnotation(value = "删除上师",type = "delete")
    public Map deleteGuru(int guruId){
        return guruService.deleteGuru(guruId);
    }



}
